/*
//   Copyright 2008 Dear Soft(http://dear-soft.com)
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
 */

package com.dear.soft.components.path;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import com.dear.soft.components.util.DialogUtil;
import com.dear.soft.components.util.ImageUtil;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ConfigureDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private static final CellConstraints cc = new CellConstraints();
	private JList list;
	private final List<String> items;

	public ConfigureDialog(Window parent, List<String> items) {
		super(parent);
		setModal(true);
		this.items = new ArrayList<String>(items);
		setLayout(new FormLayout("5dlu,100dlu:grow,2dlu,p,5dlu",
				"5dlu,60dlu:grow,5dlu"));
		this.list = new JList(items.toArray());
		this.list.getSelectionModel().setSelectionMode(
				ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		this.add(new JScrollPane(list), cc.xy(2, 2, "f,f"));
		this.add(getDeleteButton(), cc.xy(4, 2, "l,t"));
		this.setPreferredSize(new Dimension(300, 200));
		this.pack();
		DialogUtil.centerDialogInParent(this);

	}

	private JToggleButton getDeleteButton() {
		final JToggleButton delete = new JToggleButton(
				new ImageIcon(
						ImageUtil.class
								.getResource("/com/dear/soft/components/path/res/delete.gif")));
		setButtonSize(delete);
		delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (delete.isSelected()) {
					delete.setSelected(false);
					onDeleteSelected();
				}
			}
		});
		return delete;
	}

	public List<String> getItems() {
		ListModel model = list.getModel();
		List<String> result = new ArrayList<String>();
		for (int i = 0; i < model.getSize(); i++) {
			result.add(model.getElementAt(i).toString());
		}
		return result;
	}

	private void onDeleteSelected() {
		int selected[] = this.list.getSelectedIndices();
		if (selected == null || selected.length == 0) {
			return;
		}
		Arrays.sort(selected);
		for (int i = selected.length; i > 0; i--) {
			items.remove(selected[i - 1]);
		}
		list.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 1L;

			public int getSize() {
				return items.size();
			}

			public Object getElementAt(int i) {
				return items.get(i);
			}
		});
	}

	private void setButtonSize(final JToggleButton btn) {
		btn.setSize(new Dimension(20, 20));
		btn.setPreferredSize(new Dimension(20, 20));
		btn.setMaximumSize(new Dimension(20, 20));
		btn.setBorder(BorderFactory.createEmptyBorder());
	}
}
